package com.wswy.wzcx.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.blankj.utilcode.util.SizeUtils;
import com.wswy.wzcx.R;
import com.wswy.wzcx.widget.SideBar.OnChooseLetterChangedListener;

public class IndexBar extends RelativeLayout implements OnChooseLetterChangedListener {
  private TextView tvHint;


  public IndexBar(Context context, AttributeSet attrs) {
    super(context, attrs);

    tvHint = new TextView(context);
    tvHint.setTextSize(45);
    tvHint.setTextColor(Color.WHITE);
    tvHint.setGravity(Gravity.CENTER);
    tvHint.setBackgroundResource(R.drawable.index_hint_tv_bg);

    tvHint.setVisibility(GONE);

    LayoutParams lp = new LayoutParams(SizeUtils.dp2px(80),SizeUtils.dp2px(80));
    lp.addRule(CENTER_IN_PARENT);

    addView(tvHint,lp);


    SideBar sideBar = new SideBar(context);

    lp = new LayoutParams(SizeUtils.dp2px(20),LayoutParams.WRAP_CONTENT);
    lp.topMargin = lp.bottomMargin = SizeUtils.dp2px(70);
    lp.addRule(ALIGN_PARENT_RIGHT);

    addView(sideBar,lp);

    sideBar.setOnTouchingLetterChangedListener(this);

  }

  @Override
  public void onChooseLetter(String s) {
    tvHint.setText(s);
    tvHint.setVisibility(VISIBLE);
    if (onChooseLetterChangedListener != null) {
      onChooseLetterChangedListener.onChooseLetter(s);
    }
  }

  @Override
  public void onNoChooseLetter() {
    tvHint.setVisibility(GONE);
    if (onChooseLetterChangedListener != null) {
      onChooseLetterChangedListener.onNoChooseLetter();
    }
  }

  private OnChooseLetterChangedListener onChooseLetterChangedListener;

  public void setOnChooseLetterChangedListener(OnChooseLetterChangedListener onChooseLetterChangedListener) {
    this.onChooseLetterChangedListener = onChooseLetterChangedListener;
  }
}
